package com.zto.sch.advance.caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
 * @program: sch-diy-parent
 * @description:基于权重驱逐
 * @author: sch
 * @create: 2020-12-25 21:55
 **/
public class CaffeineDemoExpel2 {
    public static void main(String[] args) {
        Cache<String, String> cache = Caffeine.newBuilder()
                .maximumWeight(15)
                .weigher((k, v) -> {
                    return Integer.valueOf((String) v);
                })
                .removalListener((k, v, r) -> {
                    System.out.println("移除: key = " + k + "，cause = " + r);
                }).build();
        for (int i = 10; i <= 15; i++) {
            cache.put(String.valueOf(i), String.valueOf(i));
        }
        cache.cleanUp();
    }
}
